<?php

$mStud = $this->maintenance;
$builder = $mStud->builder;
$builder->db = $this->db;
$db = $this->db;
InputBuilder::$singleton = $builder;

if (!isEmpty(getParam('operation'))) {
    switch (getParam('operation')) {
        case 's':
            save($db);
            break;
        case 'u':
            update($db);
            break;
        case 'd':
            delete($db);
            break;
    }
}

function save($db) {
    $bd = date("d/m/Y", strtotime(getParam('BIRTH_DATE')));
    $db->query_insert('cllg_student', array(
        'ID' => sqlValue(getParam('ID'), 'int'),
        'FIRST_NAME' => sqlValue(getParam('FIRST_NAME'), 'text'),
        'SECOND_NAME' => sqlValue(getParam('SECOND_NAME'), 'text'),
        'LAST_NAME' => sqlValue(getParam('LAST_NAME'), 'text'),
        'EMAIL' => sqlValue(getParam('EMAIL'), 'text'),
        'BIRTHDATE' => sqlValue(date("y-m-d", strtotime($bd)), 'date'),
        'GENDER' => sqlValue(getParam('GENDER'), 'text'),
        'PICTURE_PATH' => sqlValue(getParam('PICTURE_PATH'), 'text'),
        'STATUS' => sqlValue(getParam('STATUS'), 'int')));

    $builder = InputBuilder::$singleton;
    $builder->saveFields(getParam('ID'));

    $_SESSION["mensaje"] = "¡Datos Guardados! ";
}

function update($db) {
    $msg = "Guardado!";
    $bd = date("d/m/Y", strtotime(getParam('BIRTH_DATE')));
    $datos = array(
        'FIRST_NAME' => sqlValue(getParam('FIRST_NAME'), 'text'),
        'SECOND_NAME' => sqlValue(getParam('SECOND_NAME'), 'text'),
        'LAST_NAME' => sqlValue(getParam('LAST_NAME'), 'text'),
        'EMAIL' => sqlValue(getParam('EMAIL'), 'text'),
        'BIRTHDATE' => sqlValue(date("y-m-d", strtotime($bd)), 'date'),
        'GENDER' => sqlValue(getParam('GENDER'), 'text'),
        'PICTURE_PATH' => sqlValue(getParam('PICTURE_PATH'), 'text'),
        'STATUS' => sqlValue(getParam('STATUS'), 'int'));

    $db->query_update('cllg_student', $datos, 'ID=' . sqlValue(getParam('ID'), 'int'));

    $builder = InputBuilder::$singleton;
    $builder->saveFields(getParam('ID'));

    $_SESSION["mensaje"] = "Informacion Guardada!";
}

function delete($db) {
    $db->query_delete('cllg_student', 'ID=' . sqlValue(getParam('ID'), 'int'));
    $fields = &unserialize($_SESSION['fields']); //campos dinamicos a borrar
    foreach ($fields as $fld) {
        $db->query_delete('cllg_table_valuebyfield', 'FK_TABLEFIELD=' . sqlValue($fld, 'text')
                . ' AND PK_RECORD=' . sqlValue(getParam('ID'), 'int'));
    }
}

?>